<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>系统设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label">SMTP服务器地址</label>
                <div class="layui-input-block">
                    <input id="smptUrl" type="text" name="smtpAddress"
                           placeholder="请输入服务器地址"  value="" class="layui-input">
                    <tip>填写SMTP服务器地址，QQ邮箱的为smtp.qq.com。</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">SMTP端口</label>
                <div class="layui-input-block">
                    <input id="port" type="text" name="smtpPort"
                            placeholder="请输入端口"  value="" class="layui-input">
                    <tip>大部分SMTP服务端口为 465</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input id="username" type="text" name="userName"
                           placeholder="请输入用户名"  value="" class="layui-input">
                    <tip>邮箱地址，如果使用的是QQ邮箱的SMTP，则填写QQ邮箱。</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input id="password" type="text" name="password"
                           placeholder="请输入密码"  value="" class="layui-input">
                    <tip>邮箱密码，如果是QQ邮箱，则填写授权码。</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">接收者</label>
                <div class="layui-input-block">
                    <input id="receiver" type="text" name="receiver"
                           placeholder="请输入邮箱"  value="" class="layui-input">
                    <tip>填写通知邮件的接收邮箱。</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">加密方式</label>
                <div class="layui-input-block">
                    <input id="none" type="radio" name="ssl" value="" title="无" checked="">
                    <input id="ssl" type="radio" name="ssl" value="SSL" title="SSL">
                    <input id="tls" type="radio" name="ssl" value="TLS" title="TLS">
                </div>
                <div class="layui-input-block">
                    <tip>QQ邮箱选择SSL，其他邮箱按需选择。</tip>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="setting">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'miniTab'], function () {
        var form = layui.form,
            $ = layui.$,
            miniTab = layui.miniTab,
            layer = layui.layer;

        $.ajax({
            url: "/smtp/getInfo",
            method: "GET",
            success: function (res) {
                $("#smptUrl").attr("value", res.content.smtpAddress);
                $("#username").attr("value", res.content.userName);
                $("#port").attr("value", res.content.smtpPort);
                $("#password").attr("value", res.content.password);
                $("#receiver").attr("value", res.content.receiver);
                if(res.content.ssl === 'SSL')
                    $("#ssl").next().click();
                else if (res.content.ssl === 'TLS')
                    $("#tls").next().click();
            },
            error: function (res) {
                layer.msg("发生错误");
                console.error(res);
            }
        });


        //监听提交
        form.on('submit(setting)', function (data) {
            data.field.nid = 1;
            $.ajax({
                url: "/smtp/updateInfo",
                method: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data.field),
                success: function (res) {
                    layer.msg("修改成功");
                    setTimeout(function () {
                        miniTab.deleteCurrentByIframe();
                    }, 2000);
                },
                error: function (res) {
                    layer.msg("修改失败");
                    console.error(res);
                }
            });
            return false;
        });

    });
</script>
</body>
</html>